package field

import (
	"bytes"
	"encoding/binary"
	"fmt"

	"gitee.com/yanwc/net/utils"
)

type (
	FirmwareVersion    uint32  // 固件版本
	FirmwareVersionRaw [4]byte // 固件版本
)

func (fvr FirmwareVersionRaw) Type() FirmwareVersion {
	return FirmwareVersion(utils.BytesToNumber[uint32](fvr[:]))
}

func (fv FirmwareVersion) Raw() (fvr FirmwareVersionRaw) {
	copy(fvr[:], utils.NumberToBytesWithBigEndian(fv)[:])
	return
}

func (fv FirmwareVersionRaw) Slice() []byte {
	return fv[:]
}

// Major.Minor.Patch.XXX
func (v FirmwareVersionRaw) String() string {
	var major uint8
	var minor uint8
	var patch uint8
	var xxx uint8
	binary.Read(bytes.NewBuffer(v[0:1]), binary.BigEndian, &major)
	binary.Read(bytes.NewBuffer(v[1:2]), binary.BigEndian, &minor)
	binary.Read(bytes.NewBuffer(v[2:3]), binary.BigEndian, &patch)
	binary.Read(bytes.NewBuffer(v[3:4]), binary.BigEndian, &xxx)
	return fmt.Sprintf("%d.%d.%d.%d", major, minor, patch, xxx)
}
